// pages/zpxx_fb/sheying.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    //获取当前位置
    chooseAddress: '',
    checked: true,
    user: [],
    city: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    //检测用户是否登录
    var user = wx.getStorageSync("userInfo")
    var city = wx.getStorageSync("city")
    if (user) {
      _this.setData({
        user: user,
        city: city
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 2000
      })
      //返回上一页
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/user/user'
        })
      }, 2000)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取地图
  fromSub(e) {
    var that = this
    var data = that.data
    var fromData = e.detail.value
    var title=false;
    if (fromData.select_type == '') {
      title ="请选择类型"
    } else if (fromData.store_name == '') {
      title = "请填写门店名称"
    } else if (data.chooseAddress == '') {
      title = "请选择门店地址"
    } else if (fromData.explain == '') {
      title = "请输入备注说明"
    }
    if(!title){
      fromData.images = data.imgs.join(',')
      fromData.location = data.chooseAddress
      fromData.longitude = data.longitude
      fromData.latitude = data.latitude
      fromData.uid = data.user.id
      fromData.city = data.city
      app.wxRequest('POST', '/Samecity/photography/photography', fromData, function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: "发布成功",
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }, function (err) {
        console.log(err)
      })
    }else{
      wx.showToast({
        title: "title",
        icon: 'none',
        duration: 2000
      })
    }
  },
  //获取企业地图信息
  onChangeAddress: function () {
    var _page = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        _page.setData({
          chooseAddress: res.name,
          latitude: res.latitude,
          longitude: res.longitude
        });
      },
      fail: function (err) {
        console.log(err)
      }
    });
  },
  //图片上传
  chooseImg: function (e) {
    let that = this;
    var imgs = that.data.imgs;
    var imgsLength = imgs.length
    wx.chooseImage({
      count: 9 - imgsLength,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var filePaths = res.tempFilePaths
        var filePathsLength = filePaths.length;
        var failNum = 0;
        for (var i = 0; i < filePathsLength; i++) {
          wx.uploadFile({
            url: app.globalData.theUrl1 + '/Samecity/Upload/uploadFile',
            filePath: filePaths[i],
            name: 'fileData',
            formData: {},
            success: (successBack) => {
              var imgs = that.data.imgs;
              var data = JSON.parse(successBack.data)
              if (data.code == 0) {
                imgs.push(data.url);
                that.setData({
                  imgs: imgs
                })
              } else {
                failNum++;
              }
            },
            fail: (failBack) => {
              failNum++;
            },
          });
        }
        wx.showToast({
          title: '总共' + filePathsLength + '张上传成功,' + failNum + '张上传失败！',
          icon: 'none'
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs
    })
  },
  //条款选项
  checkboxChange: function (e) {
    let _this = this;
    _this.setData({
      checked: _this.data.checked != false ? false : true
    })
  }
})